import 'package:flutter/material.dart';
import 'package:schoollifedemo/business/home/viewmodel/edit_friend_circle_viewmodel.dart';
import 'package:schoollifedemo/commonwidget/button_widget.dart';
import 'package:schoollifedemo/commonwidget/common_base_page.dart';
import 'package:schoollifedemo/commonwidget/common_dialog.dart';
import 'package:schoollifedemo/commonwidget/text_widget_with_overflow.dart';
import 'package:schoollifedemo/commonwidget/textfield_widget.dart';
import 'package:schoollifedemo/entity/Entity.dart';
import 'package:schoollifedemo/localization/local_text.dart';

class EditFriendCirclePage extends BaseStatefulPage {
  final String? initText;

  const EditFriendCirclePage({Key? key, this.initText}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _EditFriendCirclePageState();
  }
}

class _EditFriendCirclePageState extends BasePageState<EditFriendCirclePage> {
  bool isShowAddress = false;
  bool isButtonEnable = false;
  String msg = '';
  String location = '';
  String timestamps = '';
  final EditFriendCircleViewModel _viewModel = EditFriendCircleViewModel();

  @override
  void initState() {
    _viewModel.init();
    super.initState();
  }

  @override
  void dispose() {
    _viewModel.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.white,
        child: ListView(
          physics: const BouncingScrollPhysics(),
          children: [
            const TextWidgetWithOverflow(
              HomeLocalText.editYourFriendCircle,
              fontSize: 28,
              insideColor: Colors.transparent,
              textAlign: TextAlign.left,
              paddingLeft: 25,
              paddingTop: 25,
            ),
            Padding(
              padding: const EdgeInsets.only(
                  left: 20, right: 20, top: 10, bottom: 10),
              child: TextFieldWidget(
                insideColor: const Color(0x66D0E7FF),
                textColor: Colors.black,
                cursorColor: Colors.black,
                fontSize: 18,
                paddingLeft: 25,
                paddingRight: 25,
                paddingTop: 15,
                paddingBottom: 15,
                maxLine: 15,
                maxLength: 150,
                enableCountText: true,
                initText: widget.initText,
                keyboardType: TextInputType.multiline,
                onTextChange: (text) {
                  msg = text;
                  _checkButtonEnable(text);
                  setState(() {});
                },
              ),
            ),
            Column(
              children: [
                Container(
                  margin: const EdgeInsets.only(top: 20, left: 20, right: 20),
                  height: 30,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Checkbox(
                        value: isShowAddress,
                        onChanged: (changeValue) {
                          isShowAddress = !isShowAddress;
                          setState(() {});
                        },
                        shape: const CircleBorder(),
                      ),
                      const TextWidgetWithOverflow(
                        '展示地址信息：',
                        fontSize: 14,
                        width: 120,
                        textAlign: TextAlign.left,
                        insideColor: Colors.transparent,
                      ),
                      Expanded(
                        child: GestureDetector(
                          onTap: () {
                            isShowAddress = !isShowAddress;
                            setState(() {});
                          },
                          child: const TextWidgetWithOverflow(
                            '提瓦特大陆',
                            fontSize: 14,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  width: 80,
                  height: 50,
                  margin: const EdgeInsets.only(top: 30),
                  child: ButtonWidget(
                    enableColor: const Color(0xFF529FE8),
                    disableColor: const Color(0x80529FE8),
                    insideWidget: SizedBox(
                      width: 28,
                      height: 28,
                      child: Image.asset(
                        'assets/images/common/icon_send.png',
                      ),
                    ),
                    onButtonClick:
                        isButtonEnable == false ? null : _buttonClick,
                    radius: 50,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  void _checkButtonEnable(String text) {
    if (text.isEmpty) {
      isButtonEnable = false;
    } else {
      isButtonEnable = true;
    }
  }

  void _buttonClick() async {
    timestamps = DateTime.now().millisecondsSinceEpoch.toString();
    Map<String, dynamic>? result;
    if (isShowAddress) {
      result = await _viewModel.publishNews(
        msg: msg,
        location: location,
        timestamps: timestamps,
      );
    } else {
      result = await _viewModel.publishNews(
        msg: msg,
        timestamps: timestamps,
      );
    }
    if (result == null) {
      DialogUtils.showSimpleDialog(
        context,
        clickOutSideDismiss: true,
        title: '错误',
        widgets: const Padding(
          padding: EdgeInsets.only(left: 10, right: 10),
          child: TextWidgetWithOverflow(
            '无返回结果，服务器错误',
            fontSize: 16,
          ),
        ),
      );
    } else {
      CommonResponse response = CommonResponse.fromMap(result);
      if (response.status == 0) {
        _viewModel.backPage(context);
      } else {
        DialogUtils.showSimpleDialog(
          context,
          clickOutSideDismiss: true,
          title: '错误',
          widgets: TextWidgetWithOverflow(response.msg ?? ''),
        );
      }
    }
  }
}
